import { httpRequest } from '$common';
import { agentConfig } from './config';
import { DriverService } from '$domain/model/driver';
import { AgentAbilities, RegistratioResponse } from './type';
import axios from 'axios';

export function buildAbilities(): AgentAbilities {
  return {
    identity: agentConfig.identity,
    advertiseIp: agentConfig.advertiseIp,
    role: agentConfig.role,
    password: agentConfig.password,
    products: DriverService.getDriverNames()
  };
}

export async function registerToGateway(abilities?: AgentAbilities) {
  const { gatewayHost, gatewayPort } = agentConfig;
  return await httpRequest<RegistratioResponse>({
    url: `http://${gatewayHost}:${gatewayPort}/api/agent/registration`,
    data: abilities ? abilities : buildAbilities(),
    method: 'POST' 
  });
}

export async function osInfoReport(osData) {
  const { gatewayHost, gatewayPort } = agentConfig;
  const url = `http://${gatewayHost}:${gatewayPort}/api/agent/osInfo/${agentConfig.identity}`;
  return await axios.post(url, osData);
}